﻿// Decompiled with JetBrains decompiler
// Type: PD_Tool.DIVAFILE
// Assembly: PD_Tool_Private, Version=0.4.5.3, Culture=neutral, PublicKeyToken=null
// MVID: 8A8DE4DE-5B2D-492A-9C2E-FC536B2E689B
// Assembly location: F:\divax\data\PCSB01007\rom\PD_Tool_Private.exe

using KKtLib;
using System;
using System.IO;
using System.Security.Cryptography;

namespace PD_Tool
{
    

    public class DIVAFILE
    {
        private static readonly byte[] Key = Text.ToASCII("file access deny");
        
        public static void Decrypt(int I, string file)
        {
            KKtLib.IO io1 = KKtLib.IO.OpenReader(file);
            if (io1.ReadInt64() != 4993446652939749700L)
            {
                io1.Close();
                /*
                DIVAFILE.Encrypt(I, file);
                */
            }
            else
            {
                //Console.Title = "PD_Tool: DIVAFILE Decryptor - File: " + Path.GetFileName(file);
                int num = (int)io1.ReadUInt32();
                int length = io1.Length;
                int SetLength = io1.ReadInt32();
                byte[] buffer = new byte[length];
                io1.Seek(0L, SeekOrigin.Begin);
                using (AesManaged aesManaged = new AesManaged())
                {
                    aesManaged.Key = DIVAFILE.Key;
                    aesManaged.IV = new byte[16];
                    aesManaged.Mode = CipherMode.ECB;
                    aesManaged.Padding = PaddingMode.Zeros;
                    using (CryptoStream cryptoStream = new CryptoStream(io1.BaseStream, aesManaged.CreateDecryptor(aesManaged.Key, aesManaged.IV), CryptoStreamMode.Read))
                        cryptoStream.Read(buffer, 0, length);
                }
                KKtLib.IO io2 = KKtLib.IO.OpenWriter(file, SetLength);
                for (int index = 16; index < length && index < SetLength + 16; ++index)
                    io2.Write(buffer[index]);
                io2.Close();
            }
            //Console.Title = "PD_Tool";
        }

        public static void Encrypt(int I, string file)
        {
            KKtLib.IO io1 = KKtLib.IO.OpenReader(file);
            if (io1.ReadInt64() == 4993446652939749700L)
            {
                io1.Close();
                DIVAFILE.Decrypt(I, file);
            }
            else
            {
                //Console.Title = "PD_Tool: DIVAFILE Encryptor - File: " + Path.GetFileName(file);
                int length1 = io1.Length;
                int length2 = (int)Main.Align((long)length1, 16L);
                io1.Close();
                byte[] numArray1 = File.ReadAllBytes(file);
                byte[] buffer = new byte[length2];
                for (int index = 0; index < numArray1.Length; ++index)
                    buffer[index] = numArray1[index];
                byte[] numArray2 = new byte[length2];
                using (AesManaged aesManaged = new AesManaged())
                {
                    aesManaged.Key = DIVAFILE.Key;
                    aesManaged.IV = new byte[16];
                    aesManaged.Mode = CipherMode.ECB;
                    aesManaged.Padding = PaddingMode.Zeros;
                    using (CryptoStream cryptoStream = new CryptoStream((Stream)new MemoryStream(buffer), aesManaged.CreateEncryptor(aesManaged.Key, aesManaged.IV), CryptoStreamMode.Read))
                        cryptoStream.Read(numArray2, 0, length2);
                }
                KKtLib.IO io2 = KKtLib.IO.OpenWriter(file, buffer.Length);
                io2.Write(4993446652939749700L);
                io2.Write(length2);
                io2.Write(length1);
                io2.Write(numArray2);
                io2.Close();
            }
            //Console.Title = "PD_Tool";
        }
    }
}
